package Hander.Add;

import View.Add.AddPersonnelView;
import View.LeaderView;
import entity.Personnel;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddPersonnelViewHander implements ActionListener {
    private AddPersonnelView addPersonnelView;
    private LeaderView leaderView;
    public AddPersonnelViewHander(AddPersonnelView addPersonnelView, LeaderView leaderView) {
        this.addPersonnelView = addPersonnelView;
        this.leaderView=leaderView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("添加".equals(text)){
            PersonnelService personnelService=new PersonnelServiceImpl();
            Personnel personnel=addPersonnelView.buildAchievement();
            Boolean addresult=personnelService.add(personnel.getAllattributes(),addPersonnelView.getTablesName());
            if(addresult){
                leaderView.reloadTable(addPersonnelView.getTablesName());
                JOptionPane.showMessageDialog(addPersonnelView,"添加成功");
                addPersonnelView.dispose();
            } else{
                System.out.println("添加错误！");
                JOptionPane.showMessageDialog(addPersonnelView,"添加失败");
            }
        }
    }
}
